package com.example.sale.Service.imply;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.example.sale.Service.IMessageService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import java.util.HashMap;
import java.util.Map;
@Service
public class MessageService implements IMessageService {

    private static  final String accessKeyId= "LTAI5tHWqPCpKEGQLDjHfbMn";

    private static  final  String accessKeySecret="DRom3iu9lq1IzKf7IqwX5AFXY0Acn4";


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    @SneakyThrows
    public Client createClient(String accessKeyId, String accessKeySecret){
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }


    /**
     * 发送短信
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     * @return 返回结果
     */
    @SneakyThrows
    public Map<String,Object> sendMessage(String phoneNumber, String code) {
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        Client client = createClient(accessKeyId, accessKeySecret);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("wzd的网站验证码")
                .setTemplateCode("SMS_465170909")
                .setPhoneNumbers(phoneNumber)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
        SendSmsResponseBody body = response.getBody();
        String code1 = body.getCode();
        System.out.println(sendSmsRequest);
        System.out.println(response);
        System.out.println(code1);
        Map<String,Object> map = new HashMap<>();
        if ("OK".equals(code1)){
            map.put("status","true");
            return map;
//            return BaseResult.ok();
        }
        map.put("status","false");
        return map;
//        return new BaseResult(500,body.getMessage(),null);
    }
}
